<?php namespace Phpcmf\Controllers\Member;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Hdbm extends \Phpcmf\Member\Mform
{

    public function index() {
        $this->_Member_List();
    }

    public function add() {
        $data = \Phpcmf\Service::L('input')->post('data'); // 数据
        //获取活动发起人数据
        $data_huodong = \Phpcmf\Service::M()->table('1_huodong')->get($data['hd_id']);
        $jifen = (int)$data['huodongjifen'];
        $money = (int)$data['hdfy'];
        //检测是否已经报过名
        $huodong_renshu = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')
		    ->where('cid', $data_huodong['id'])
			->where('tieid', $data['tieid'])
			->where('quanid', $data['quanid'])
			->where('shenhe','1')
			->where('buy_uid',$this->uid)
			->countAllResults();
		if($huodong_renshu){
		    $this->_json(0, '你已经报过名啦');
		}
		//线上付费
		if($data['hdfs']== '4'){
		    if((int)$data['hdfy'] > $this->member['money']){
		        $this->_json(0, '余额不足请充值');
		    } else {
		        //减去用户的报名费用
		        \Phpcmf\Service::M('member')->add_money($this->uid, -$money);
		        \Phpcmf\Service::M('Pay')->add_paylog([
                    'uid' => $this->uid,
                    'username' => $data_huodong['uid'],
                    'touid' => $data_huodong['uid'],
                    'tousername' => '',
                    'mid' => 'system', // 特征字符串，system表示系统
                    'title' => '报名《'.$data_huodong['title']."》活动消费",
                    'value' => -$money, // 交易金额，正数表示增加金额，负数表示消费金额
                    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
                    'status' => 1, // 1表示交易成功,0表示未成功
                    'result' => '报名《'.$data_huodong['title']."》活动消费",
                    'paytime' => SYS_TIME, // 付款时间，默认当前时间
                    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
                ]);
		        $data['feiyong'] = $money;
		        $data['fufeijifen'] = 10;// 已经付费
		        //新增活动发布人的资金，并且先冻结资金
		        //\Phpcmf\Service::M('Member')->add_freeze($data_huodong['uid'], $money);
		    }
			
		}
		// 线上积分
		if($data['hdfs']== '5'){
		    if((int)$data['huodongjifen'] > $this->member['score']){
		        //积分不足
		        $this->_json(0, '积分不足',$this->member);
		    } else {
		        //扣取积分
		        \Phpcmf\Service::M('member')->add_score($this->uid, -$jifen, $note = '报名活动《'.$data['title'].'》消费', $url = '', $mark = '', $count = 0);
		        $data['fufeijifen'] = 31;// 已经积分
		        \Phpcmf\Service::M('member')->add_score($data_huodong['uid'], $jifen, $note = '报名活动《'.$data['title'].'》赚取', $url = '', $mark = '', $count = 0);
		    }
		    $data['huodongjifen'] = $data_huodong['huodongjifen'];
			
		}
		$data['shenhe'] = 1;
		$this->_Member_Add();
		 //

    }

    public function edit() {
        $this->_Member_Edit();
    }

    public function order_edit() {
        $this->_Member_Order();
    }

    public function del() {
        $this->_Member_Del();
    }
    //退出活动
    public function tuichu() {
        //
        $data = \Phpcmf\Service::L('input')->post('data');
        $id = (int)$data['hd_id'];
        //获取活动发起人数据
        $data_huodong = \Phpcmf\Service::M()->table('1_huodong')->get($id);
        if(!$data_huodong){
            $this->_json(0, '活动退出数据不存在',$id);
        }
        $tui_uid = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('cid',$id)->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe','1')->countAllResults();
        $tui_uid_data = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('cid',$id)->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe','1')->get()->getRowArray();
        if(!$tui_uid){
            $this->_json(0, '没有退出活动数据',$data);
        }
        $jifen = (int)$tui_uid_data['huodongjifen'];
        $money = (int)$tui_uid_data['feiyong'];
        
        //修改原来的订单
        if($data['hdfs'] == '4'){
            $neirong = [
    			'fufeijifen' => 12,
    			'shenhe' => 0,
    		];
            \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe',1)->update($neirong);
            //给用户退回费用
            \Phpcmf\Service::M('member')->add_money($this->uid, $money);
            //解封活动用户的结冻金额
            //\Phpcmf\Service::M('Member')->cancel_freeze($data_huodong['uid'], -$money);
            \Phpcmf\Service::M('Pay')->add_paylog([
                    'uid' => $this->uid,
                    'username' => '',
                    'touid' => $data_huodong['uid'],
                    'tousername' => '',
                    'mid' => 'system', // 特征字符串，system表示系统
                    'title' => '报名《'.$data_huodong['title']."》活动退回费用",
                    'value' => $money, // 交易金额，正数表示增加金额，负数表示消费金额
                    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
                    'status' => 1, // 1表示交易成功,0表示未成功
                    'result' => '报名《'.$data_huodong['title']."》活动退回费用",
                    'paytime' => SYS_TIME, // 付款时间，默认当前时间
                    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
                ]);
        } elseif ($data['hdfs'] == '5'){
            $neirong = [
    			'fufeijifen' => 32,
    			'shenhe' => 0,
    		];
            \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe',1)->update($neirong);
            
            \Phpcmf\Service::M('member')->add_score($this->uid, $jifen, $note = '报名活动《'.$data['title'].'》退回', $url = '', $mark = '', $count = 0);
		        $data['fufeijifen'] = 31;// 已经积分
		        \Phpcmf\Service::M('member')->add_score($data_huodong['uid'], -$jifen, $note = '报名活动《'.$data['title'].'》退取', $url = '', $mark = '', $count = 0);
        } else {
            $neirong = [
    			'fufeijifen' => 200,
    			'shenhe' => 0,
    		];
            \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe',)->update($neirong);
        }
        $this->_json(1, '退出成功');
    }
    //完成活动
    public function wancheng() {
        $data = \Phpcmf\Service::L('input')->post('data');
        $id = (int)$data['hd_id'];
        //获取活动发起人数据
        $data_huodong = \Phpcmf\Service::M()->table('1_huodong')->get($id);
        if(!$data_huodong){
            $this->_json(0, '活动退出数据不存在',$id);
        }
        $tui_uid = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('cid',$id)->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe','1')->countAllResults();
        if(!$tui_uid){
            $this->_json(0, '没有退出活动数据',$data);
        }
        $jifen = (int)$data['huodongjifen'];
        $money = (int)$data['hdfy'];
        
        //修改原来的订单
        if($data['hdfs'] == '4'){
            $neirong = [
    			'fufeijifen' => 100,
    		];
            \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe',1)->update($neirong);
            //给用户退回费用
            \Phpcmf\Service::M('member')->add_money($data_huodong['uid'], $money);
            //解封活动用户的结冻金额
            //\Phpcmf\Service::M('Member')->cancel_freeze($data_huodong['uid'], -$money);
            \Phpcmf\Service::M('Pay')->add_paylog([
                    'uid' => $data_huodong['uid'],
                    'username' => '',
                    'touid' => $this->uid,
                    'tousername' => '',
                    'mid' => 'system', // 特征字符串，system表示系统
                    'title' => '报名《'.$data_huodong['title']."》活动费用",
                    'value' => $money, // 交易金额，正数表示增加金额，负数表示消费金额
                    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
                    'status' => 1, // 1表示交易成功,0表示未成功
                    'result' => '报名《'.$data_huodong['title']."》活动费用",
                    'paytime' => SYS_TIME, // 付款时间，默认当前时间
                    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
                ]);
        } elseif ($data['hdfs'] == '5'){
            $neirong = [
    			'fufeijifen' => 100,
    		];
            \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe',1)->update($neirong);
            
            \Phpcmf\Service::M('member')->add_score($data_huodong['uid'], $jifen, $note = '报名活动《'.$data['title'].'》', $url = '', $mark = '', $count = 0);
		        
		        \Phpcmf\Service::M('member')->add_score($data_huodong['uid'], -$jifen, $note = '报名活动《'.$data['title'].'》', $url = '', $mark = '', $count = 0);
        } else {
            $neirong = [
    			'fufeijifen' => 100,
    		];
            \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('buy_uid', $this->uid)->where('shenhe',)->update($neirong);
        }
        $this->_json(1, '确认成功');
    }
    //报名会员列表
    public function uid_list(){
		$id = (int)\Phpcmf\Service::L('input')->get('id'); // 帖子id
		$cid = (int)\Phpcmf\Service::L('input')->get('cid'); // 圈子id
		$hdid = (int)\Phpcmf\Service::L('input')->get('hdids'); // 活动id
		$data_huodong = \Phpcmf\Service::M()->table('1_huodong')->get($hdid);
		
		if($this->uid == $data_huodong['uid']){
		    $data_0 = \Phpcmf\Service::M()->table('1_huodong_form_hdbm')->where('tieid', $id)->where('quanid', $cid)->getAll();
            //循环数据
            $data = [];
            foreach($data_0 as $key => $val){
                // 名称
                $userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $val['uid'])->get()->getRowArray();
                $user = dr_member_info($val['uid']);
                if($userweixin){
                	$mingcheng = $userweixin['nickname'];
                } else if($user['name']) {
                	$mingcheng = $user['name'];
                } else {
                	$mingcheng = $user['username'];
                }
                /*
                正常|0
                已付费|10
                退款中|11
                已退款|12
                已付积分|31
                退回积分|32
                圆满完成|100
                退出|200
                */
                if($val['fufeijifen']==1){
                    $fufeijifen = '正常';
                } elseif ($val['fufeijifen']==10){
                    $fufeijifen = '已付费';
                } elseif ($val['fufeijifen']==11){
                    $fufeijifen = '退款中';
                } elseif ($val['fufeijifen']==12){
                    $fufeijifen = '已退款';
                } elseif ($val['fufeijifen']==31){
                    $fufeijifen = '已付积分';
                } elseif ($val['fufeijifen']==32){
                    $fufeijifen = '退回积分';
                } elseif ($val['fufeijifen']==100){
                    $fufeijifen = '圆满完成';
                } elseif ($val['fufeijifen']==200){
                    $fufeijifen = '退出';
                } else {
                    $fufeijifen = '异常';
                }
                if($val['shoujihaoma'] == "undefined"){
                    $shoujihao ='';
                } else {
                    $shoujihao = $val['shoujihaoma'];
                }
                if($val['weixinhao'] == "undefined"){
                    $weixinhao ='';
                } else {
                    $weixinhao = $val['weixinhao'];
                }
                if($val['cph'] == "undefined"){
                    $cph ='';
                } else {
                    $cph = $val['cph'];
                }
                $sfz = [];
                //$val['sfz']
                $data_sfz = dr_string2array($val['sfz']);
                foreach($data_sfz as $key2 => $val2){
                    $sfz[] = [
    					'ming' => $val2[1],
    					'hao' => $val2[2],
			    	];
                }
                $data[] = [
                    'uid' => $val['uid'],
                    'name' => $mingcheng,
                    'shoujihaoma' => $shoujihaoma,
                    'weixinhao' => $weixinhao,
                    'zhuangtai' => $fufeijifen,
					'shijian' => dr_date($val['inputtime'], 'Y-m-d H:i:s'),
					'cph' => $cph,
					'sfz' => $sfz,
					'bmfl' => $val['bmfl']
                ];
            }
		    $this->_json(1, '读取成功',$data);
		} else {
		    $this->_json(0, '无权访问');
		}
	}
}
